package com.jt.config;

import java.util.LinkedHashMap;


import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.jt.sys.service.realm.ShiroUserRealm;


@Configuration
@ComponentScan(value="com.jt",includeFilters={@Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false)
@EnableWebMvc //启用MVC的默认配置
public class AppServletConfig extends WebMvcConfigurerAdapter{
	/**配置视图解析器*/
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp("/WEB-INF/pages/", ".html");
	}
	//transaction
		/**
		 * 配置事物管理对象
		 * @param dataSource
		 * @return
		 */
	
	@Bean("shiroFilter")
	public ShiroFilterFactoryBean ShiroFilterFactoryBean(@Autowired SecurityManager securityManager){
		ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
		bean.setSecurityManager(securityManager);
		//当此用户是一个非认证用户,需要先登录进行认证
		bean.setLoginUrl("/doLoginUI.do");
		LinkedHashMap<String , String> fcMap=new LinkedHashMap<>();
		fcMap.put("/bower_components/**", "anon");
		fcMap.put("/build/**", "anon");
		fcMap.put("/dist/**", "anon");
		fcMap.put("/plugins/**", "anon");
		fcMap.put("/doLogin.do", "anon");
		fcMap.put("/user/doFindRoleIdsByUserId.do", "anon");
		fcMap.put("/doLogout.do", "logout");
		fcMap.put("/**", "authc");//必须授权才能访问
		bean.setFilterChainDefinitionMap(fcMap);
		return bean;
	}
	/**
	 * 配置shiro生命周期管理器
	 * @return
	 */
	@Bean("lifecycleBeanPostProcessor")
	public LifecycleBeanPostProcessor LifecycleBeanPostProcessor(){
		LifecycleBeanPostProcessor lBeanPostProcessor = new LifecycleBeanPostProcessor();
		return lBeanPostProcessor;
	}
	/**
	 * shiro注解应用
	 * @return
	 */
	@Bean
	@DependsOn(value="lifecycleBeanPostProcessor")
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
		DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
		return defaultAdvisorAutoProxyCreator;
	}
	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Autowired SecurityManager securityManager){
		AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
		authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
		return  authorizationAttributeSourceAdvisor;
	}
	@Bean("securityManager")
	public DefaultWebSecurityManager newDefaultWebSecurityManager(@Autowired ShiroUserRealm userRealm){
		DefaultWebSecurityManager dsm=new DefaultWebSecurityManager();
		dsm.setRealm(userRealm);
		return dsm;
	}
}
